﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Platform.ObjectModel.Serializers
{
	public abstract class DataObjectStreamer
	{


		protected DataObjectStreamer( DataObjectName qualifiedName )
		{
			Debug.Assert( qualifiedName != null );

			this.QualifiedName = qualifiedName;
		}

		public DataObjectName QualifiedName
		{ get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public abstract byte[] ToStream( object value );

		/// <summary>
		/// 
		/// </summary>
		/// <param name="valueType"></param>
		/// <param name="stream"></param>
		/// <returns></returns>
		public abstract object FromStream( Type valueType, byte[] stream );

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public string ToBase64String( object value )
		{
			byte[] stream = this.ToStream( value );

			if ( stream == null )
				return null;
			return Convert.ToBase64String( stream );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="valueType"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public object FromBase64String( Type valueType, string value )
		{
			if ( value == null )
				return null;

			byte[] stream = Convert.FromBase64String( value );
			return this.FromStream( valueType, stream );
		}
	}

}
